namespace Plugins.Engine
{
    using System;
    using System.Collections.ObjectModel;
    using System.Diagnostics.CodeAnalysis;
    using Plugins.Interfaces;
    using Plugins.Serializer;

    [Serializable]
    public sealed class PluginActivationCollection : Collection<PluginActivationData>, IPluginActivationCollection
    {
        #region IPluginActivationContainer Members

        public bool Contains(Guid pluginId)
        {
            foreach(IPluginActivation plugin in this)
            {
                if (plugin.Id == pluginId) return  true;
            }
            return false;
        }

        public bool IsActive(Guid pluginId)
        {
            foreach(IPluginActivation plugin in this)
            {
                if (plugin.Id == pluginId)
                {
                    if (string.IsNullOrEmpty(plugin.Activation)) return false;
                    return true;
                }
            }
            return false;
        }

        #endregion
    }

    [Serializable]
    public sealed class PluginActivationManager : IPluginActivationManager
    {
        private PluginActivationCollection _plugins = new PluginActivationCollection();

        public PluginActivationCollection Plugins
        {
            get
            {
                return this._plugins;
            }
        }

        public IPluginActivationCollection Items
        {
            get
            {
                return this._plugins;
            }
        }

        [SuppressMessage("Microsoft.Performance", "CA1812")]
        private class XmlFormatter : PluginTypeSerializer<PluginActivationManager>
        {
            public override void AddOverrides(IPluginTypeFormatter formatter)
            {
                AddType(formatter, "activation");
                AddArray(formatter, "Plugins", "plugins");
                AddArrayItem(formatter, "Plugins", "plugin", typeof(PluginActivationData));
                PluginActivationData.Formatter.AddOverrides(formatter);
            }
        }
    }

    [Serializable]
    public sealed class PluginActivationData : IPluginActivation//, IPluginDeserializable<PluginHostId>
    {
        [SuppressMessage("Microsoft.Usage", "CA2211")]
        [SuppressMessage("Microsoft.Security", "CA2104")]
        public static readonly IPluginRuntimeFormatter Formatter = new XmlFormatter();
        [SuppressMessage("Microsoft.Usage", "CA2211")]
        [SuppressMessage("Microsoft.Security", "CA2104")]
        public static readonly IPluginStringSerializer<PluginActivationData> XmlSerializer = new PluginUnicodeStringSerializer<PluginActivationData>();

        private Guid _host;
        private Guid _id;
        private string _activation;

        public PluginActivationData()
        {
        }

        public Guid Host
        {
            get
            {
                return _host;
            }
            set
            {
                _host = value;
            }
        }
        public Guid Id
        {
            get
            {
                return _id;
            }
            set
            {
                _id = value;
            }
        }

        public string Activation { get { return _activation; } set { _activation = value; } }

        //public string Serialize()
        //{
        //    return XmlSerializer.Serialize(this, Formatter);
        //}

        //public void Deserialize(string s)
        //{
        //    Initialize(XmlSerializer.Deserialize(s, Formatter));
        //}

        //public void Initialize(PluginHostId obj)
        //{
        //    this._key = obj._key;
        //    this._value = obj._value;       
        //    this.Value = obj.Value;
        //}
       
        private class XmlFormatter : PluginTypeSerializer<PluginActivationData>
        {
            public override void AddOverrides(IPluginTypeFormatter formatter)
            {
                base.AddType(formatter, "plugin");
                base.AddAttribute(formatter, "Host", "host", typeof(Guid));
                base.AddAttribute(formatter, "Id", "id", typeof(Guid));
                base.AddElement(formatter, "Activation", "activation");
            }
        }
    }
}
